#include "data_forward.h"
#include "encode_decode.h"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

// #include "ringbuf.h"
#include "ringbuffer.h"


uint8_t lora_buffer[BUFFER_SIZE] = {0};
uint16_t buffer_used = 0;
uint16_t head_index = 0; // 头指针位置 指向 0x5a
uint16_t end_index = 0; //尾指针位置 指向 0x69
DATA_PARSE_STATUS gStatus = PS_INIT_F;


extern uint16_t RS485_RX_Index;
extern uint8_t RS485_RX_Buffer[100];
// #define RINGBUF_SIZE 1024

RingBuffer *gRingbuf;

void Lora_Data_Recv_Handler(uint8_t data) {
    int rc = RingBuffer_write(gRingbuf, &data, 1);
    if(rc > 100) {
        RingBuffer_empty(gRingbuf);
        RS485_Send_Data(&data, 1);
    }
    // Data_Parse(data);
}

void RS485_No_Data_Handler() {
    uint8_t i=0;
    if(RS485_RX_Index>0) {
        delay_ms(20);
        RS485_H;
        delay_ms(2);
        // disableInterrupts();
        for(i=0;i<RS485_RX_Index;i++) {
            RS485_Send_Byte(RS485_RX_Buffer[i]);
        }
        
        delay_ms(2);
        // enableInterrupts();
        RS485_L;
        RS485_RX_Index = 0;
    }
}

void RS485_Recv_Handler(uint8_t data) {
    
    lora_buffer[buffer_used++] = data;
    if(buffer_used >11) {
#ifdef GCC_DEBUG
#else
        // RS485_Send_Data(lora_buffer, buffer_used-1);
#endif

    RS485_Send_Data(lora_buffer, buffer_used+1);
    memset(lora_buffer, 0x00, 1024);
    buffer_used = 0;
    }
}

void Data_Parse(uint8_t data) {
    //printf("%x \n", data);
    switch(gStatus) {
        case PS_INIT_F:
            if(data == 0x5a) {
                gStatus = PS_HEAD_F;
            } 
            break;
        case PS_HEAD_F:
            if(data == 0x55) {
                gStatus = PS_HEAD_FOUND;
            } 
            else if (data == 0x5a)
            {
                break;
            } else {
                gStatus = PS_INIT_F;
            }
            break;
        case PS_HEAD_FOUND:
            if(data == 0x6a) {
                gStatus = PS_END_F;
            }
            break;
        case PS_END_F:
            if(data == 0x69) {
                gStatus = PS_END_FOUND;
            } 
            else if (data == 0x6a)
            {
                break;
            } else {
                gStatus = PS_HEAD_FOUND;
            }
            break;
    }
    lora_buffer[buffer_used++] = data;
    if(gStatus == PS_HEAD_F) {
        head_index = buffer_used -1;
    }
    if(gStatus == PS_END_FOUND) {
        end_index = buffer_used -1;
        //uint8_t validate_buffer[1024];
        //memcpy(validate_buffer, lora_buffer+head_index, end_index-head_index+1);
        uint8_t *pHead = lora_buffer + head_index;
        int16_t len = end_index - head_index + 1;
        Validate_Status vs = isPackageValid(pHead, len);


        if(vs == VS_OK) {
            RS485_Send_Data(pHead, len);
            memset(lora_buffer, 0x00, 1024);

            // Lora_Data_Pack *pDataPack = Create_Recv_Lora_Data_Pack(pHead, len);
            head_index = 0;
            end_index = 0;
            buffer_used = 0;
            // RS485_Send_Data(pHead, len);
            len = 0;
            vs = VS_FAIL;
#ifdef LOCAL_TEST
            printf("%x ", pDataPack->Payload);
#else
            
            
            //RS485_Send_Data(pDataPack->Payload, len-11);
#endif

        }
    }
}


